package com.xmall.order.controller;

import com.xmall.aspect.OperateRecord;
import com.xmall.aspect.RepeatSubmit;
import com.xmall.define.base.Result;
import com.xmall.define.dict.OperateTypeEnum;
import com.xmall.order.define.dto.platform.OrderRefundDtoForAdmin;
import com.xmall.order.define.request.refund.*;
import com.xmall.order.service.IOrderRefundService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/service/order/refund")
public class OrderRefundController {

    @Resource
    private IOrderRefundService orderRefundService;

    /**
     * 售后列表
     */
    @GetMapping("/listForAdmin/{orderId}")
    public Result<OrderRefundDtoForAdmin> listForAdmin(@PathVariable("orderId") Long orderId) {
        return Result.success(orderRefundService.listForAdmin(orderId));
    }

    /**
     * 售前申请退款
     */
    @OperateRecord(type = OperateTypeEnum.ADD)
    @RepeatSubmit
    @PostMapping("/applyRefund")
    public Result applyRefund(@Validated @RequestBody OrderApplyRefundRequest request){
        orderRefundService.applyRefund(request);
        return Result.success();
    }

    /**
     * 售后申请退款
     */
    @OperateRecord(type = OperateTypeEnum.ADD)
    @RepeatSubmit
    @PostMapping("/applyReturn")
    public Result applyReturn(@Validated @RequestBody OrderApplyReturnRequest request){
        orderRefundService.applyReturn(request);
        return Result.success();
    }

    /**
     * 售前退款：同意
     */
    @OperateRecord(type = OperateTypeEnum.UPDATE)
    @RepeatSubmit
    @PostMapping("/approveRefund/{id}")
    public Result approveRefund(@PathVariable("id") Long id) {
        orderRefundService.approveRefund(id);
        return Result.success();
    }

    /**
     * 售后退款：同意
     */
    @OperateRecord(type = OperateTypeEnum.UPDATE)
    @RepeatSubmit
    @PostMapping("/approveReturn/{id}")
    public Result approveReturn(@PathVariable("id") Long id) {
        orderRefundService.approveReturn(id);
        return Result.success();
    }

    /**
     * 售前退款：拒绝
     */
    @OperateRecord(type = OperateTypeEnum.UPDATE)
    @RepeatSubmit
    @PostMapping("/refuseRefund")
    public Result refuseRefund(@Validated @RequestBody OrderRefuseRefundRequest request) {
        orderRefundService.refuseRefund(request);
        return Result.success();
    }

    /**
     * 售后退款：拒绝
     */
    @OperateRecord(type = OperateTypeEnum.UPDATE)
    @RepeatSubmit
    @PostMapping("/refuseReturn")
    public Result refuseReturn(@Validated @RequestBody OrderRefuseReturnRequest request) {
        orderRefundService.refuseReturn(request);
        return Result.success();
    }

    /**
     * 退货发货
     */
    @OperateRecord(type = OperateTypeEnum.UPDATE)
    @RepeatSubmit
    @PostMapping("/returnDelivery")
    public Result returnDelivery(@Validated @RequestBody OrderReturnDeliveryRequest request) {
        orderRefundService.returnDelivery(request);
        return Result.success();
    }

    /**
     * 退货收货
     */
    @OperateRecord(type = OperateTypeEnum.UPDATE)
    @RepeatSubmit
    @PostMapping("/returnDeliveryReceive/{id}")
    public Result returnDeliveryReceive(@PathVariable("id") Long id) {
        orderRefundService.returnDeliveryReceive(id);
        return Result.success();
    }
}
